﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CauseSetting.aspx.cs" Inherits="School_CauseSetting" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>学校课程配置</title>
    <link href="../Common/css/main.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    #load{width:500px;height:25px;border:1px #000 solid;}
    #loading{position:absolute;z-index:2;height:23;filter:progid:DXImageTransform.microsoft.gradient(gradienttype=1,startColorStr=white,endColorStr=#39867b);}
    #loadtext{position:absolute;z-index:3;width:100%;height:100%;line-height:23px;text-align:center;}
    </style>
    
    <script type="text/javascript" src="../Common/Js/jquery.js"></script>

    <script type="text/javascript" src="../Common/Js/common.js"></script>

    <script type="text/javascript" src="../Common/Js/msg.js"></script>
    
    <script type="text/javascript" src="../Common/Js/jsUtil.js"></script>
    
    <script type="text/javascript" language="javascript">
    
    //每次保存前的选择与未选择的CheckBox的ID记录，当保存失败后，进行恢复
    var CheckCause = new Array();
    var UnCheckCause = new Array();
    
    //提交给后台程序，当前的选择与未选择的情况。
    var delCause = new Array();
    var chooseCause = new Array();
    
    function Load()
    {
        PageMethods.InitSchoolCause(function(succ){
            if (succ.length > 0)
            {
                //表格有2列,获取表格行数
                var maxCol = 4;
                var maxRow = Math.ceil( succ.length/maxCol );
                
                var strTable = [""];
                strTable.push("<table border=\"1\" cellpadding=\"0\" cellspacing=\"1\" class=\"useList\" bordercolor=\"#c3dce5\" width=\"96%\" height=\"" + maxRow*40 + "\">");
                
                for(var i = 0; i < maxRow; i++)
                {
                     strTable.push("<tr class=\"tableTitle\">");
                      for(var j = 0; j < maxCol; j++)
                      {
                         //如果学校列表越界
                         if (i*maxCol + j >= succ.length) break;
                         
                         if (succ[i*maxCol + j].State == 1)
                         {
                            CheckCause.push(succ[i*maxCol + j].RID);
                            var checkbox = "&nbsp;<input type=checkbox id="+ succ[i*maxCol + j].RID +" checked=1>&nbsp;&nbsp;" + succ[i*maxCol + j].CauseName;
                         }    
                         else
                         {
                            UnCheckCause.push(succ[i*maxCol + j].RID);
                            var checkbox = "&nbsp;<input type=checkbox id="+ succ[i*maxCol + j].RID +">&nbsp;&nbsp;" + succ[i*maxCol + j].CauseName;
                         }
                         strTable.push("<td width=\"" + Math.ceil( 100/maxCol ) + "%\" style=\"text-align: left;padding-left:5px;\">" + checkbox +"</td>");
                      }
                     strTable.push("</tr>"); 
                }
                
                strTable.push("</table>"); 
                
                var obj = document.getElementById("tableDiv");
                obj.innerHTML = strTable.join("");
            }
        },function(err){//异步失败
        showMassgeBox("提示",CAUSE_QUERY_FAIL,1,null); })	
    }
    
    function Resume()
    {   
        //遍历所有的CheckBox     
        var checks = document.getElementById("tableDiv").getElementsByTagName("input");
        for(var i = 0; i < checks.length; i++)
        {
            if (checks[i].type == "checkbox")
            {
                //判断保存前的被选中的CheckBox的ID
                for(var j =0; j < CheckCause.length; j++)
                {
                    //如果当前的CheckBox应该是被选中的，那么将其状态改为Checked
                    if (checks[i].id == CheckCause[j])
                    {
                        checks[i].checked = 1;
                    }
                }
                
                //遍历保存前不应该被中的CheckBox的ID
                for(var g =0; g < UnCheckCause.length; g++)
                {
                    //如果当前的Checkbox的ID属于不应该被选中的ID，则将其状态改为UnChecked。
                    if (checks[i].id == UnCheckCause[g])
                    {
                        checks[i].checked = 0;
                    }
                }  
            }          
        }
    }
    
    function Save()
    {
        //遍历所有CheckBox
        var inputs = document.getElementById("tableDiv").getElementsByTagName("input");
        for(var i =0 ; i < inputs.length ; i++)
        {
            //判断CheckBox是否选择，如果选择了， 没有选择则
            if (inputs[i].type == "checkbox")
            {
                
                if (inputs[i].checked == 1)
                    chooseCause.push(inputs[i].id);
                else
                    delCause.push(inputs[i].id);
            }
        }
        
        PageMethods.SaveCauseSetting(chooseCause,delCause, function(succ)
        {

            
            if (succ == -1)
            {
                showMassgeBox("提示","删除的课程正在使用，不能删除",1,null);
                
                Resume();               

            }
            else if (succ == 0)
            {
                showMassgeBox("提示","保存设置失败",1,null);
                Resume(); 
            }
            else if (succ == 1)
            {
                showMassgeBox("提示","保存成功",1,null);
                
                CheckCause = new Array();
                CheckCause = chooseCause;
                
                UnCheckCause = new Array();
                UnCheckCause = delCause;
            }
            
            chooseCause = new Array();
            delCause = new Array();
        })
        
    }
    
    </script>
</head>
<body onload="Load();">
    <div class="mainWarp">
        <div class="pageBody">
    <form id="form1" runat="server">
        <div>  
            <ul class="butList">
            <li>
                <input id="Button1" type="button" value="保存" onclick="Save();" class="commonBut" />
            </li>
            </ul>
        </div> 
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
            </asp:ScriptManager>
        </div>
        <div id="tableDiv" style="margin: 20px auto 20px auto; text-align: center">
            &nbsp;</div>
    </form>
    </div>
    </div>
</body>
</html>
